
import '../entity/book_entity.dart';
import '../main.dart';
import '../util/toast_util.dart';
import 'api_url.dart';
import 'http_helper.dart';

// Future findBook(bookNo) async {
//
//   var response = await HttpHelper.instance.sip2Post(findBookUrl, data: {'device_id': settingEntity.deviceNo, 'book_no': bookNo});
//   if(response['code'] == 200){
//     var data  = BookEntity.fromJson(response['result']);
//     return (200,data);
//   }else if(response['code'] == 429){
//     return (429,null);
//   }else{
//     return (500,null);
//   }
// }

Future findBook(bookNo) async {

  var (status,response) = await HttpHelper.instance.post(findBookUrl, data: {'more': settingEntity.deviceNo, 'book_no': bookNo});
  if(status == 200){
    if(response['status'] == 200){
      var data  = BookEntity.fromJson(response['data']);
      return (200,data);
    }
    toastWarn('${response['error']}');
    return (500,null);
  }else{
    toastWarn("${response['error']}");
    return (500,null);
  }
}


Future login(account,psw) async {
  var (status,response) = await HttpHelper.instance.post(loginUrl, data: {'device_id':(settingEntity.deviceNo??'').replaceAll('device_id=', ''), 'account': account, 'password': psw});

  if(status == 200){
    return response['user_id'];
  }else {
    toastWarn("$response");
    return null;
  }
}

Future<BookEntity?> findRfidBook(bookNo) async {
  var (status,response) = await HttpHelper.instance.post(findRfidBookUrl, data: {'device_id': (settingEntity.deviceNo??'').replaceAll('device_id=', ''), 'book_no': bookNo});
  if(status == 200){
    var data  = BookEntity.fromJson(response);
    return data;
  }else{
    toastWarn("$response");
    return null;
  }
}

Future<List<BookEntity>?> findRfidPlaceBook(placeNo) async {
  var (status,response) = await HttpHelper.instance.post(findRfidPlaceBookUrl, data: {'device_id': (settingEntity.deviceNo??'').replaceAll('device_id=', ''), 'place_no': placeNo});
  if(status == 200){
    List<BookEntity> list = [];
    for (var element in (response as List)) {
      var book = BookEntity.fromJson(element);
      book.hasApi = true;
      list.add(book);
    }
    return list;
  }else{
    toastWarn("$response");
    return null;
  }
}


Future<String?> findPlace(placeNo) async {
  var (status,response) = await HttpHelper.instance.post(findPlaceUrl, data: {'device_id': (settingEntity.deviceNo??'').replaceAll('device_id=', ''), 'place_no': placeNo});

  if(status == 200){
    return response['place_name'];
  }else {
    toastWarn('$response');
    return null;
  }
}
Future<bool> on(books,placeNo) async {
  var (status,response) = await HttpHelper.instance.post(upUrl,data: {'device_id': (settingEntity.deviceNo??'').replaceAll('device_id=', ''), 'place_no': placeNo,'user_id':currentTask.uid,'books':books});

  if(status == 200){
    return true;
  }else {
    toastWarn('$response');
    return false;
  }
}
Future<bool> off(books) async {
  var (status,response) = await HttpHelper.instance.post(offUrl,data: {'device_id': (settingEntity.deviceNo??'').replaceAll('device_id=', ''), 'user_id':currentTask.uid,'book_no':books});

  if(status == 200){
    return true;
  }else {
    toastWarn('$response');
    return false;
  }
}

Future<bool> move(place,toPlace,books) async {
  var (status,response) = await HttpHelper.instance.post(moveUrl,data: {'device_id': (settingEntity.deviceNo??'').replaceAll('device_id=', ''), 'user_id':currentTask.uid,
    'old_place_no':place,'new_place_no':toPlace,'book_no':books});
  if(status == 200){
    return true;
  }else {
    toastWarn('$response');
    return false;
  }
}
Future<List<BookEntity>?> query(keyword,page,limit) async {
  var (status,response) = await HttpHelper.instance.post(queryUrl,data: {'device_id': (settingEntity.deviceNo??'').replaceAll('device_id=', ''), 'page':page,'keywords':keyword,'limit':limit});
  if(status == 200 && !response['empty']){
    List<BookEntity> list = [];
    for (var element in (response['list'] as List)) {
      var book = BookEntity.fromJson(element);
      list.add(book);
    }
    return list;
  }else{
    return null;
  }
}

Future<bool> inventory(place,hasBooks,borrowedBooks,loseBooks,wrongBooks,unKnowBooks) async {
  var (status,response) = await HttpHelper.instance.post(inventoryUrl,data: {'device_id': (settingEntity.deviceNo??'').replaceAll('device_id=', ''), 'user_id':currentTask.uid,
    'place_no':place,'correct_book_list':hasBooks,'borrow_book_list':borrowedBooks,"miss_book_list":loseBooks,"wrong_book_list":wrongBooks,
  "unknow_book_list":unKnowBooks});
  if(status == 200){
    return true;
  }else {
    toastWarn('$response');
    return false;
  }
}

